package nc.pubimpl.credit.creditcheck;

import nc.impl.credit.pub.ActionConstrictUtil;
import nc.impl.pubapp.env.BSContext;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.pubitf.credit.creditcheck.IFlowCreditCheck4M30;
import nc.vo.credit.creditcheck.func.CreditCheckFuncEnum;
import nc.vo.credit.engrossmaintain.pub.action.M30EngrossAction;
import nc.vo.credit.pub.CreditDimensionVO;
import nc.vo.credit.pub.ExamItemVO;
import nc.vo.credit.pub.ExamVO;
import nc.vo.credit.pub.env.SessionVariableEnum;
import nc.vo.credit.util.ProcessPfUtilActionConstrictVOUtils;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pub.pf.PfUtilActionConstrictVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.PubAppTool;
import nc.vo.scmpub.res.billtype.SOBillType;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderHVO;
import nc.vo.so.m30.entity.SaleOrderVO;
import nc.vo.so.m30.revise.entity.SaleOrderHistoryVO;

public class FlowCreditCheck4M30
  implements IFlowCreditCheck4M30
{
  public UFDouble examOverCreditForOrder(AggregatedValueObject aggVO, boolean ignoreControl) throws BusinessException {
    UFDouble retValue = new UFDouble(-1000000000);
    
    try {
      ExamVO examVO = getBillVO(aggVO);

      
      boolean auditFlowFuncFlag = false;
      
      Object o = BSContext.getInstance().getSession(SessionVariableEnum.AuditFlowFuncFlag
          .getValue());
      if (null != o)
      {
        auditFlowFuncFlag = true;
      }
      
      if (auditFlowFuncFlag) {
        AuditFlowCreditCheck checker = new AuditFlowCreditCheck(false, true);
        ExamItemVO[] examitemvos = examVO.getExamitemvos();
        retValue = checker.exam(examitemvos);

      
      }else {


        
        String[] actions = { M30EngrossAction.M30Insert.getValue(), M30EngrossAction.M30Approve.getValue(), M30EngrossAction.M30ReVise.getValue() };
        
        ActionConstrictUtil util = new ActionConstrictUtil();
        
        String method = (ignoreControl == true) ? CreditCheckFuncEnum.ExamOverCreditForOrderCheck.getValue() : CreditCheckFuncEnum.ExamOverCreditForOrder.getValue();

        
        PfUtilActionConstrictVO constrictVO = util.getConstrictVO(examVO.getVtrantypecode(), examVO
            .getCbiztypeid(), examVO.getOperator(), examVO
            .getPk_org(), actions, method);
        
        if (null != constrictVO) {
          FlowCreditCheck checker = new FlowCreditCheck(false, ignoreControl, true, aggVO);
          
          ExamItemVO[] examitemvos = examVO.getExamitemvos();
          
          String value = constrictVO.getValue();
          if (PubAppTool.isNull(value)) {
            
            Object obj = ProcessPfUtilActionConstrictVOUtils.getValue(constrictVO, CreditCheckFuncEnum.ExamOverCreditForOrder
                .getValue());
            if (obj != null) {
              value = obj.toString();
            }
          } 
          
          String ysf = constrictVO.getYsf();
          if (ysf == null)
          {
            ysf = ProcessPfUtilActionConstrictVOUtils.getYsf(constrictVO, CreditCheckFuncEnum.ExamOverCreditForOrder
                .getValue());
          }
          retValue = checker.exam(examitemvos, ysf, value);
        }
      
      } 
    } catch (Exception e) {
      ExceptionUtils.marsh(e);
    } 
    return retValue;
  }


  
  public UFDouble examOverCreditForOrderApprove(AggregatedValueObject aggVO) throws BusinessException {
    UFDouble retValue = new UFDouble(-1000000000);
    try {
      ExamVO examVO = getBillVO(aggVO);
      
      AuditFlowCreditCheck checker = new AuditFlowCreditCheck(false, true);
      ExamItemVO[] examitemvos = examVO.getExamitemvos();
      retValue = checker.exam(examitemvos);
    
    }
    catch (Exception e) {
      ExceptionUtils.marsh(e);
    } 
    return retValue;
  }


  
  public UFDouble examOverCreditRateForOrder(AggregatedValueObject aggVO) throws BusinessException {
    UFDouble retValue = new UFDouble(-1000000000);
    try {
      ExamVO examVO = getBillVO(aggVO);
      
      boolean auditFlowFuncFlag = false;
      
      Object o = BSContext.getInstance().getSession(SessionVariableEnum.AuditFlowFuncFlag
          .getValue());
      if (null != o)
      {
        auditFlowFuncFlag = true;
      }
      if (auditFlowFuncFlag) {
        AuditFlowCreditCheck checker = new AuditFlowCreditCheck(true, true);
        ExamItemVO[] examitemvos = examVO.getExamitemvos();
        retValue = checker.exam(examitemvos);

      
      }
      else {


        
        String[] actions = { M30EngrossAction.M30Insert.getValue(), M30EngrossAction.M30Approve.getValue(), M30EngrossAction.M30ReVise.getValue() };
        
        ActionConstrictUtil util = new ActionConstrictUtil();
        
        PfUtilActionConstrictVO constrictVO = util.getConstrictVO(examVO.getVtrantypecode(), examVO
            .getCbiztypeid(), examVO.getOperator(), examVO
            .getPk_org(), actions, CreditCheckFuncEnum.ExamOverCreditRateForOrder
            .getValue());
        if (null != constrictVO) {
          FlowCreditCheck checker = new FlowCreditCheck(true);
          ExamItemVO[] examitemvos = examVO.getExamitemvos();
          String value = constrictVO.getValue();
          if (PubAppTool.isNull(value)) {
            
            Object obj = ProcessPfUtilActionConstrictVOUtils.getValue(constrictVO, CreditCheckFuncEnum.ExamOverCreditRateForOrder
                .getValue());
            if (obj != null) {
              value = obj.toString();
            }
          } 
          String ysf = constrictVO.getYsf();
          if (ysf == null)
          {
            ysf = ProcessPfUtilActionConstrictVOUtils.getYsf(constrictVO, CreditCheckFuncEnum.ExamOverCreditRateForOrder
                .getValue());
          }
          retValue = checker.exam(examitemvos, ysf, value);
        }else {
        	AuditFlowCreditCheck checker = new AuditFlowCreditCheck(true, true); 
        	ExamItemVO[] examitemvos = examVO.getExamitemvos(); 
        	retValue = checker.exam(examitemvos);
        }
      
      } 
    } catch (Exception e) {
      ExceptionUtils.marsh(e);
    } 
    return retValue;
  }






  
  public ExamVO getBillVO(AggregatedValueObject aggVO) {
    SaleOrderVO vo = (SaleOrderVO)aggVO;













    
    return getExamVOByQuery(vo);
  }

  
  private ExamItemVO[] getDimensionVOs(SaleOrderBVO[] items, SaleOrderHVO hvo) {
    int len = items.length;
    ExamItemVO[] examitemvos = new ExamItemVO[len];
    for (int i = 0; i < len; i++) {
      CreditDimensionVO dimensionvo = new CreditDimensionVO();
      dimensionvo.setCchanneltypeid(hvo.getCchanneltypeid());
      dimensionvo.setCcustomerid(hvo.getCcustomerid());
      dimensionvo.setCemployeeid(hvo.getCemployeeid());
      dimensionvo.setCfinanceorgid(items[i].getCsettleorgid());
      dimensionvo.setCorigcurrencyid(hvo.getCorigcurrencyid());
      dimensionvo.setCprodlineid(items[i].getCprodlineid());
      dimensionvo.setCsaledeptid(hvo.getCdeptid());
      dimensionvo.setCsaleorgid(hvo.getPk_org());
      dimensionvo.setVtrantypecode(hvo.getCtrantypeid());
      
      dimensionvo.setCinvoicecustid(hvo.getCinvoicecustid());
      
      ExamItemVO examitemvo = new ExamItemVO();
      examitemvo.setNmny(items[i].getNorigtaxmny());
      examitemvo.setDimensionVO(dimensionvo);
      
      examitemvos[i] = examitemvo;
    } 
    return examitemvos;
  }

  
  private ExamVO getExamVOByQuery(SaleOrderVO vo) {
    String pk_bill = vo.getPrimaryKey();
    
    SaleOrderVO retVO = null;
    
    if (!PubAppTool.isNull(pk_bill)) {
      
      Object chistrantypeid = vo.getParentVO().getAttributeValue("chistrantypeid");
      
      if (chistrantypeid != null && SOBillType.Order30R
        .getCode().equals(chistrantypeid)) {
        BillQuery<SaleOrderHistoryVO> queryhis = new BillQuery<SaleOrderHistoryVO>(SaleOrderHistoryVO.class);
        
        SaleOrderHistoryVO[] hisVOs = (SaleOrderHistoryVO[])queryhis.query(new String[] { vo
              .getPrimaryKey() });
        
        if (hisVOs != null && hisVOs.length > 0) {
          SaleOrderHistoryVO saleOrderHistoryVO = hisVOs[0];
        }
      } else {
        
        BillQuery<SaleOrderVO> query = new BillQuery<SaleOrderVO>(SaleOrderVO.class);
        
        SaleOrderVO[] retVOs = (SaleOrderVO[])query.query(new String[] { vo
              .getPrimaryKey() });
        
        if (retVOs != null && retVOs.length > 0) {
          retVO = retVOs[0];
        }
      } 
      if (retVO == null)
      {
        ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
            .getStrByID("4032001_0", "04032001-0134"));
      }
    }
    else {
      
      ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("4032001_0", "04032001-0134"));
    } 
    return getExamVOFromVO(retVO);
  }
























  
  private ExamVO getExamVOFromVO(SaleOrderVO vo) {
    SaleOrderHVO hvo = vo.getParentVO();
    ExamVO examVO = new ExamVO();
    examVO.setCbiztypeid(hvo.getCbiztypeid());
    examVO.setOperator(hvo.getBillmaker());
    examVO.setPk_org(hvo.getPk_org());
    examVO.setCtrantypeid(hvo.getCtrantypeid());
    examVO.setVtrantypecode(hvo.getVtrantypecode());
    
    SaleOrderBVO[] items = vo.getChildrenVO();
    ExamItemVO[] examitemvos = getDimensionVOs(items, hvo);
    
    examVO.setExamitemvos(examitemvos);
    
    return examVO;
  }


  
  public UFDouble examOverCreditForOrder(AggregatedValueObject aggVO) throws BusinessException { return examOverCreditForOrder(aggVO, false); }
}